const Controller = require('egg').Controller;

class Appointments extends Controller {

  

    async findByUserId() {
        const { ctx } = this;
        
        try {
          const results = await this.service.appointments.findByUserId();
          if (results && results.length > 0) {
            ctx.body = { success: true, data: results };
          } else {
            ctx.body = { success: false, message: '没有找到相关预约信息' };
          }
        } catch (error) {
          ctx.body = { success: false, message: '查询出错', error: error.message };
        }
    }


    async allList() {
        const { ctx } = this;
        try {
          const results = await this.service.appointments.allList();
          if (results && results.length > 0) {
            ctx.body = { success: true, data: results };
          } else {
            ctx.body = { success: false, message: '没有找到相关预约信息' };
          }
        } catch (error) {
          ctx.body = { success: false, message: '查询出错', error: error.message };
        }
    }
    async create() {
        const { ctx } = this;
        const result = await this.service.appointments.create();
        if (result.affectedRows === 1) {
          ctx.body = { success: true, message: 'Appointment created successfully.' };
        } else {
          ctx.body = { success: false, message: 'Failed to create appointment.' };
        }
      }
    
      // Retrieve all appointments
      async list() {
        const { ctx } = this;
        const results = await this.service.appointments.list('appointments');
        ctx.body = { success: true, data: results };
      }
    
      // Retrieve a single appointment
      async read() {
        const { ctx } = this;
        const appointment = await this.service.appointments.read();
        if (appointment) {
          ctx.body = { success: true, data: appointment };
        } else {
          ctx.body = { success: false, message: 'Appointment not found.' };
        }
      }
    
      async readoctor() {
        const { ctx } = this;
        const results = await this.service.appointments.readoctor();
        if (results && results.length > 0) {
          ctx.body = { success: true, data: results };
        } else {
          ctx.body = { success: false, message: '没有找到相关预约信息' };
        }
      }

      // Update an appointment
      async update() {
        const { ctx } = this;
        const result = await this.service.appointments.update();
        if (result.affectedRows === 1) {
          ctx.body = { success: true, message: 'Appointment updated successfully.' };
        } else {
          ctx.body = { success: false, message: 'Failed to update appointment.' };
        }
      }
    
      // Delete an appointment
      async delete() {
        const { ctx } = this;
        const result = await this.service.appointments.delete();
        if (result.affectedRows === 1) {
          ctx.body = { success: true, message: 'Appointment deleted successfully.' };
        } else {
          ctx.body = { success: false, message: 'Failed to delete appointment.' };
        }
      }
}
module.exports = Appointments;
